#!/bin/sh

# Display usage
cpack_usage()
{
  cat <<EOF
Usage: $0 [options]
Options: [defaults in brackets after descriptions]
  --help            print this message
  --prefix=dir      directory in which to install
  --include-subdir  include the @CPACK_PACKAGE_INSTALL_DIRECTORY@ subdirectory
  --exclude-subdir  exclude the @CPACK_PACKAGE_INSTALL_DIRECTORY@ subdirectory
EOF
  exit 1
}

cpack_echo_exit()
{
  echo $1
  exit 1
}

# Display version
cpack_version()
{
  echo "@CPACK_PACKAGE_NAME@ Installer Version: @CPACK_PACKAGE_VERSION@, Copyright (c) @CPACK_PACKAGE_VENDOR@"
}

# Helper function to fix windows paths.
cpack_fix_slashes ()
{
  echo "$1" | sed 's/\\/\//g'
}

interactive=TRUE
cpack_include_subdir=""
for a in "$@CPACK_AT_SIGN@"; do
  if echo $a | grep "^--prefix=" > /dev/null 2> /dev/null; then
    cpack_prefix_dir=`echo $a | sed "s/^--prefix=//"`
    cpack_prefix_dir=`cpack_fix_slashes "${cpack_prefix_dir}"`
  fi
  if echo $a | grep "^--help" > /dev/null 2> /dev/null; then
    cpack_usage 
  fi
  if echo $a | grep "^--version" > /dev/null 2> /dev/null; then
    cpack_version 
    exit 2
  fi
  if echo $a | grep "^--include-subdir" > /dev/null 2> /dev/null; then
    cpack_include_subdir=TRUE
  fi
  if echo $a | grep "^--exclude-subdir" > /dev/null 2> /dev/null; then
    cpack_include_subdir=FALSE
  fi
done

if [ "x${cpack_include_subdir}x" != "xx" ]
then
  interactive=FALSE
fi

cpack_version
echo "This is a self-extracting archive."
toplevel="`pwd`"
if [ "x${cpack_prefix_dir}x" != "xx" ]
then
  toplevel="${cpack_prefix_dir}"
fi

echo "The archive will be extracted to: ${toplevel}"

if [ "x${interactive}x" = "xTRUEx" ]
then
  echo ""
  echo "If you want to stop extracting, please press <ctrl-C>."

  if [ "x${cpack_include_subdir}x" = "xx" ]
  then
    echo "By default @CPACK_PACKAGE_NAME@ will be installed in:"
    echo "  \"${toplevel}/@CPACK_PACKAGE_INSTALL_DIRECTORY@\""
    echo "Do you want to include the subdirectory @CPACK_PACKAGE_INSTALL_DIRECTORY@?"
    echo "Saying no will install in: \"${toplevel}\" [Yn]: "
    read line leftover
    cpack_include_subdir=TRUE
    case ${line} in
      n* | N*)
        cpack_include_subdir=FALSE
    esac
  fi
fi

if [ "x${cpack_include_subdir}x" = "xTRUEx" ]
then
  toplevel="${toplevel}/@CPACK_PACKAGE_INSTALL_DIRECTORY@"
  mkdir -p "${toplevel}"
fi
echo
echo "Using target directory: ${toplevel}"
echo "Extracting, please wait..."
echo ""

# take the archive portion of this file and pipe it to tar
# the NUMERIC parameter in this command should be one more
# than the number of lines in this header file
# there are tails which don't understand the "-n" argument, e.g. on SunOS
# OTOH there are tails which complain when not using the "-n" argument (e.g. GNU)
# so at first try to tail some file to see if tail fails if used with "-n"
# if so, don't use "-n"
use_new_tail_syntax="-n"
tail $use_new_tail_syntax +1 "$0" > /dev/null 2> /dev/null || use_new_tail_syntax=""

tail $use_new_tail_syntax +###CPACK_HEADER_LENGTH### "$0" | gunzip | (cd "${toplevel}" && tar xf -) || cpack_echo_exit "Problem unpacking @CPACK_PACKAGE_FILE_NAME@"

echo "Unpacking finished successfully"

echo "Installing icon files..."
local_install_path="$HOME/.local/share"
icon_install_path="${local_install_path}/icons/hicolor"
icon_source_path="${toplevel}/Resources/GitAhead.iconset"
mkdir -p "${icon_install_path}/16x16/apps" && cp -f "${icon_source_path}/icon_16x16.png" "${icon_install_path}/16x16/apps/gitahead.png"
mkdir -p "${icon_install_path}/32x32/apps" && cp -f "${icon_source_path}/icon_32x32.png" "${icon_install_path}/32x32/apps/gitahead.png"
mkdir -p "${icon_install_path}/64x64/apps" && cp -f "${icon_source_path}/icon_64x64.png" "${icon_install_path}/64x64/apps/gitahead.png"
mkdir -p "${icon_install_path}/128x128/apps" && cp -f "${icon_source_path}/icon_128x128.png" "${icon_install_path}/128x128/apps/gitahead.png"
mkdir -p "${icon_install_path}/256x256/apps" && cp -f "${icon_source_path}/icon_256x256.png" "${icon_install_path}/256x256/apps/gitahead.png"
mkdir -p "${icon_install_path}/512x512/apps" && cp -f "${icon_source_path}/icon_512x512.png" "${icon_install_path}/512x512/apps/gitahead.png"

echo "Writing desktop file..."
desktop_install_path="${local_install_path}/applications"
mkdir -p "${desktop_install_path}" && cat <<EOF > "${desktop_install_path}/gitahead.desktop"
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=GitAhead
Icon=gitahead
Terminal=false
Exec=$SHELL -c ${toplevel}/GitAhead
Categories=Development;RevisionControl;
EOF

exit 0
#-----------------------------------------------------------
#      Start of TAR.GZ file
#-----------------------------------------------------------;

